package com.killer.blog.controller;

import com.killer.blog.pojo.Article;
import com.killer.blog.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class ArticleController {

    @Autowired
    public ArticleService articleService;

    @RequestMapping("/listArticles")
    public String listArticles(@RequestParam(value = "page",defaultValue = "1") Integer page,
                               @RequestParam(value = "pageLength",defaultValue = "2")Integer pageLength,
                               Model model){
        Page<Article> pages = articleService.listArticle(page, pageLength);
        model.addAttribute("articles", pages.getContent());
        model.addAttribute("count", pages.getTotalPages());
        model.addAttribute("currentPage", page);
        model.addAttribute("prePage", page<=1?1:page-1);
        model.addAttribute("nextPage", page>=pages.getTotalPages()?pages.getTotalPages():page+1);
        return "client/index";
    }
}
